package com.boot.management_boot.controller;

import com.alibaba.fastjson.JSON;
import com.boot.management_boot.Utils.Result;
import com.boot.management_boot.pojo.Admin_allinfo;
import com.boot.management_boot.pojo.User;
import com.boot.management_boot.pojo.User_info;
import com.boot.management_boot.service.imp.BootServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class bootController {
    //自动注入
    @Autowired
    BootServiceImpl service;

    //登录
    @RequestMapping("/user_login")
    @ResponseBody
    public Map user_login(String username, String password){
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(username.length()<=0||password.length()<=0){
            hashMap.put("code","500");
            hashMap.put("msg","请输入参数");
            return hashMap;
        }
        User login = service.login(username, password);
        if(login==null){
            hashMap.put("code","101");
            hashMap.put("msg","账号或密码错误");
            return hashMap;
        }else{

            hashMap.put("code","200");
            hashMap.put("msg","success");
            hashMap.put("data",login);
            return hashMap;
        }
    }
    //查询用户所有数据
    @RequestMapping("/queryuserInfo")
    @ResponseBody
    public Map queryuserInfo(Integer uid){
        HashMap<Object, Object> hashMap = new HashMap<>();

        if(uid==null){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }
        List<User_info> user_infos = service.queryuserInfo(uid);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",user_infos);
        return hashMap;
    }

    //查询用户当月所有数据
    @RequestMapping("/queryuserInfo_m")
    @ResponseBody
    public Map queryuserInfo_m(Integer uid){
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(uid==null){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }
        List<User_info> user_infos = service.queryuserInfo_m(uid);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",user_infos);
        return hashMap;
    }
    //修改密码
    @RequestMapping("/update_user_password")
    @ResponseBody
    public Map update_user_password(String username, String password){
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(username.length()<=0||password.length()<=0){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }
        int i = service.update_user_password(username, password);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",i);
        return hashMap;
    }
    //判断用户名是否存在
    @RequestMapping("/query_user_name")
    @ResponseBody
    public Map query_user_name(String username){
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(username.length()<=0){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }
        int i = service.query_user_name(username);
        if(i>0){
            hashMap.put("code","101");
            hashMap.put("msg","用户名已存在");
            hashMap.put("data",i);
            return hashMap;
        }else{
            hashMap.put("code","200");
            hashMap.put("msg","success");
            hashMap.put("data",i);
            return hashMap;
        }
    }
    //添加用户 admin
    @RequestMapping("/add_user")
    @ResponseBody
    public Map add_user(String username,String password,String edu){
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(username.length()==0&&password.length()==0&&edu.length()==0){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }
        int i = service.add_user(username, password, edu);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",i);
        return hashMap;
    }


    //查询用户当月所有数据
    @RequestMapping("/queryuserInfo_get_time")
    @ResponseBody
    public Map queryuserInfo_get_time(Integer uid,String time) throws ParseException {
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(uid==null){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }
        List<User_info> user_infos = service.queryuserInfo_get_time(uid,time);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",user_infos);
        return hashMap;
    }

    //查询用户当月所有数据
    @RequestMapping("/queryuserInfo_get_time1")
    @ResponseBody
    public Map queryuserInfo_get_time1(Integer uid,String time,String time2) throws ParseException {
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(uid==null){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }
        List<User_info> user_infos = service.queryuserInfo_get_time1(uid,time,time2);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",user_infos);
        return hashMap;
    }

    //查询所有用户总计的数据
    @RequestMapping("/admin_queryuserallinfo")
    @ResponseBody
    public Map admin_queryuserallinfo(){
        HashMap<Object, Object> hashMap = new HashMap<>();
        List<Map> maps = service.admin_queryuserallinfo();
        if(maps==null){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }else {
            hashMap.put("code","200");
            hashMap.put("msg","success");
            hashMap.put("data",maps);
            return hashMap;
        }
    }

    //修改密码
    @RequestMapping("/admin_updatepassword")
    @ResponseBody
    public Map admin_updatepassword(String username, String password,Integer id){
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(username.length()<=0||password.length()<=0){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }
        int i = service.admin_updatepassword(username, password,id);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",i);
        return hashMap;
    }


    //管理员删除用户修改状态
    @RequestMapping("/admin_deluser")
    @ResponseBody
    public Map admin_deluser(Integer id){
        HashMap<Object, Object> hashMap = new HashMap<>();
        int i = service.admin_deluser(id);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",i);
        return hashMap;
    }

    //编辑用户数据
    @RequestMapping("/admin_info_f_t")
    @ResponseBody
    public Map admin_info_f_t(Integer id, String info_time, Integer info_liushui, Integer info_shouyi,Integer column){
        HashMap<Object, Object> hashMap = new HashMap<>();
        service.admin_info_f_t(id,info_time,info_liushui,info_shouyi,column);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        return hashMap;
    }


    //删除用户
    @RequestMapping("/del_user")
    @ResponseBody
    public Map del_user(Integer id){
        HashMap<Object, Object> hashMap = new HashMap<>();
        int i = service.del_user(id);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",i);
        return hashMap;
    }

    //根据时间区间删除用户数据
    @RequestMapping("/del_userinfo_time")
    @ResponseBody
    public Map del_userinfo_time(Integer id, String time, String time1) throws ParseException {
        HashMap<Object, Object> hashMap = new HashMap<>();
        System.out.println(id+time+time1);
        service.del_userinfo_time(id,time,time1);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        return hashMap;
    }

    //管理员修改用户的额度
    @RequestMapping("/admin_updateedu")
    @ResponseBody
    public Map admin_updateedu(Integer id, String username, String edu) throws ParseException {
        HashMap<Object, Object> hashMap = new HashMap<>();
        int i = service.admin_updateedu(edu, username, id);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",i);
        return hashMap;
    }
    //超管查询所有管理员信息
    @RequestMapping("/super_admin_user")
    @ResponseBody
    public Map super_admin_user(){
        HashMap<Object, Object> hashMap = new HashMap<>();
        List<Map> maps = service.super_admin_user();
        if(maps==null){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }else {
            hashMap.put("code","200");
            hashMap.put("msg","success");
            hashMap.put("data",maps);
            return hashMap;
        }
    }

    //超管添加管理员
    @RequestMapping("/add_admin_user")
    @ResponseBody
    public Map add_admin_user(String username,String password){
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(username.length()==0&&password.length()==0){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }
        int i = service.add_admin_user(username, password);
        hashMap.put("code","200");
        hashMap.put("msg","success");
        hashMap.put("data",i);
        return hashMap;
    }

    //超管查询所有管理员信息
    @RequestMapping("/query_user")
    @ResponseBody
    public Map query_user(String username){
        HashMap<Object, Object> hashMap = new HashMap<>();
        Map maps = service.query_user(username);
        if(maps==null){
            hashMap.put("code","500");
            hashMap.put("msg","fail");
            return hashMap;
        }else {
            hashMap.put("code","200");
            hashMap.put("msg","success");
            hashMap.put("data",maps);
            return hashMap;
        }
    }

}
